package com.zz.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zz.common.Log;
import com.zz.common.R;
import com.zz.entity.Investigation;
import com.zz.service.InvestigationService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import javax.validation.constraints.NotNull;

/**
 * @author 周紫
 */
@RestController
@RequestMapping("/investigation")
@Slf4j
@CrossOrigin
@Validated
public class InvestigationController {

    @Autowired
    private InvestigationService investigationService;

    /**
    * 新增问卷调查
     * 测试成功！！
     * 周紫
    * */
    @PostMapping("/add")
    @CacheEvict(value = "investigationCache",allEntries = true)
    @Log(title = "普通用户：新增问卷调查", dataLocationType = "body")
    public R<String> addInvestigation(@RequestBody @Valid Investigation investigation){
        return investigationService.add(investigation);
    }

    /**
     * 问卷调查分页展示
     * 周紫
     * @param page
     * @param pageSize
     * @return
     */
    @RequiresRoles("leader")
    @GetMapping("/page")
    @Cacheable(value="investigationCache",key = "'investigation'+#page+#pageSize+#activityId",sync = true)
    @Log(title = "分页展示问卷调查",dataLocationType = "link")
    public R<Page> pageInvestigation(@RequestParam @NotNull(message = "起始页码不能为空") int page,
                        @RequestParam @NotNull(message = "每页最大数不能为空") int pageSize,
                        @RequestParam @NotNull(message = "活动编码不能为空") int activityId) {
        return investigationService.page(page, pageSize, activityId);
    }
}
